import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import UserProfile from '../views/UserProfile.vue'
import Sections from '../views/Sections.vue'
import Section from '../views/Section.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/profile',
    name: 'UserProfile',
    component: UserProfile,
    meta: { requiresAuth: true }
  },
  {
    path: '/sections',
    name: 'Sections',
    component: Sections
  },
  {
    path: '/section/:id',
    name: 'Section',
    component: Section,
    props: true
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  const user = localStorage.getItem('user');
  
  if (to.meta.requiresAuth && !user) {
    // 如果需要认证但未登录，重定向到首页
    next('/');
  } else {
    next();
  }
});

export default router